﻿using Marbles.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marbles.CMS.ContentTypes.Tasks
{
    public partial class TaskEntry : System.Web.UI.UserControl
    {

        public Unit Width
        {
            get
            {
                object obj = ViewState["Width"];
                if (obj == null) { return new Unit(100, UnitType.Percentage); }
                return (Unit)obj;
            }
            set
            {
                ViewState["Width"] = value;
                txtSubject.Width = new Unit(value.Value - 100, UnitType.Pixel);
                txtNotes.Width = new Unit(value.Value - 20, UnitType.Pixel);
            }
        }

        public int TaskId
        {
            get
            {
                object obj = ViewState["TaskId"];
                if (obj == null) { return 0; }
                return Convert.ToInt32(obj);
            }
            set { ViewState["TaskId"] = value; }
        }

        public string Username
        {
            get
            {
                object obj = ViewState["Username"];
                if (obj == null) { return this.Page.User.Identity.Name; }
                return obj.ToString();
            }
            set { ViewState["Username"] = value; }
        }

        public TaskEntryItem Item
        {
            get
            {
                return GetEntry();
            }
            set
            {
                SetEntry(value);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                InitialiseForm();
            }
        }

        public void LoadItem(int TaskId)
        {
            this.TaskId = TaskId;
            ContentManager mgr = new ContentManager();
            this.Item = mgr.GetTaskEntry(this.TaskId, this.Username);
        }

        public void SaveItem()
        {
            ContentManager mgr = new ContentManager();
            mgr.SaveTask(this.Item);
        }

        private void SetEntry(TaskEntryItem value)
        {
            drpStatus.SelectedValue = value.TaskStatusId.ToString();
            drpPriority.SelectedValue = value.TaskPriorityId.ToString();
            txtDueDate.Text = value.DueDate.ToString("dd/MM/yyyy");
            txtStartDate.Text = value.StartDate.ToString("dd/MM/yyyy");
            txtSubject.Text = value.Subject;
            txtNotes.Text = value.Notes;
        }

        private TaskEntryItem GetEntry()
        {
            DateTime value = DateTime.MinValue;
            TaskEntryItem item = new TaskEntryItem(this.TaskId, this.Username);
            item.TaskPriorityId = Convert.ToInt32(drpPriority.SelectedValue);
            item.TaskStatusId = Convert.ToInt32(drpStatus.SelectedValue);
            item.Subject = txtSubject.Text;
            if (!DateTime.TryParse(txtDueDate.Text, out value)) { value = DateTime.MaxValue; }
            item.DueDate = value;
            if (!DateTime.TryParse(txtStartDate.Text, out value)) { value = DateTime.MaxValue; }
            item.StartDate = value;
            item.Notes = txtNotes.Text;
            return item;
        }

        private void InitialiseForm()
        {
            ContentManager mgr = new ContentManager();
            drpStatus.DataSource = mgr.GetTaskStates();
            drpPriority.DataSource = mgr.GetTaskPriorities();
            drpStatus.DataBind();
            drpPriority.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveItem();
            ClearForm();
        }

        public void ClearForm()
        {
            txtDueDate.Text = DateTime.Now.AddDays(7).ToString("dd/MM/yyyy");
            ctlExtDueDate.SelectedDate = DateTime.Now.AddDays(7);
            ctlExtStartDate.SelectedDate = DateTime.Now;
            txtStartDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtSubject.Text = string.Empty;
            txtNotes.Text = string.Empty;
            drpPriority.ClearSelection();
            drpStatus.ClearSelection();
            drpPriority.SelectedValue = "1";
            this.TaskId = 0;
            if (this.TaskId == 0) { btnSave.Text = "Add"; }
        }
    }
}